/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.dndControllers.gantt.taskItem;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.interfaces.Decisionmaker;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;
/**
 * <p>
 * The drag controller for the progression handle of a {@link org.tbjd.client.guis.gantt.items.GanttTaskItem GanttTaskItem}
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class GanttTaskItemProgressDragController extends PickupDragController implements Decisionmaker {

	private Widget sender = null;
	private Map<Integer, Object> map = null;

	public GanttTaskItemProgressDragController(AbsolutePanel boundaryPanel, boolean allowDroppingOnBoundaryPanel) {
		super(boundaryPanel, allowDroppingOnBoundaryPanel);
	}

	public void dragStart() {
		super.dragStart();
	}

	public void dragMove() {
		super.dragMove();
		this.map = new HashMap<Integer, Object>();
		this.sender = this.context.draggable;
		this.map.put(NotificationsConstants.TASK_PROGRESS_DRAGGING, this.sender);
		decide(this.map);
	}

	public void dragEnd() {
		super.dragEnd();
		this.map = new HashMap<Integer, Object>();
		this.sender = this.context.draggable;
		this.map.put(NotificationsConstants.TASK_UPDATED, this.sender);
		decide(this.map);
	}

	public void decide(Object action) {
		try {
			delegate(action);
		} catch (NoUpperDecisionmakerFoundException e) {
			e.toString();
		} catch (NoUpperMediatorFoundException e) {
			e.toString();
		}
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		System.out.println("GanttTaskItemProgressDragController.delegate()");

		if (((Map<Integer, ?>) action).containsKey(NotificationsConstants.TASK_UPDATED)) {
			// create a map with the new progress position and give it to the GanttTaskItem container
			this.map = new HashMap<Integer, Object>();
			this.map.put(NotificationsConstants.TASK_UPDATED, this.sender.getElement().getOffsetLeft());
		}

		if (((Map<Integer, ?>) action).containsKey(NotificationsConstants.TASK_PROGRESS_DRAGGING)) {
			this.map = new HashMap<Integer, Object>();
			this.map.put(NotificationsConstants.TASK_PROGRESS_DRAGGING, this.context.mouseX);
		}

		// passes info to the first responsible
		Widget parent = ((Widget) this.sender).getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(this.map);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			throw new NoUpperDecisionmakerFoundException();
		}
	}

	public void handle(Object action) {
	}
}
